package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.MyApplication.Companion.context
import app.qrcode.browser.hearttoheart.R
import app.qrcode.browser.hearttoheart.activities.VideoPlayActivity


/**
 * Created by DomenCai on 2022/3/30.
 */
class ImagesRVAdapter : RecyclerView.Adapter<ImagesRVAdapter.ImagesViewHolder>() {

    private val imageRVList: MutableList<Images> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: ArrayList<Images>) {
        imageRVList.clear()
        imageRVList.addAll(newList)
        notifyDataSetChanged()
    }


    inner class ImagesViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val image: ImageView = view.findViewById(R.id.search_head)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImagesViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_rourse_pic, parent, false)
        val viewHolder = ImagesViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.bindingAdapterPosition
            val image = imageRVList[position]
            if (image.image == R.mipmap.kejianmore) {
                val intent = Intent(context, VideoPlayActivity::class.java)
                intent.putExtra("video_name", R.raw.primaryscience)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context.startActivity(intent)
            }
            if (image.image == R.mipmap.kejian4) {
                val intent = Intent(context, VideoPlayActivity::class.java)
                intent.putExtra("video_name", R.raw.primarymaths)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context.startActivity(intent)
            }
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ImagesViewHolder, position: Int) {
        val imagesRV = imageRVList[position]
        holder.image.setImageResource(imagesRV.image)
    }

    override fun getItemCount(): Int = imageRVList.size
}